package com.codefox.service;

import com.codefox.entity.TbDoctor;
import com.codefox.mapper.TbDoctorMapper;
import com.codefox.util.PageUtil;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 医生服务层
 *
 * @author xiaobiye
 */
@Service
public class TbDoctorService {
    @Autowired
    SqlSessionFactory sqlSessionFactory;
    @Autowired
    PageUtil<TbDoctor> pageUtil;
    /**
     * 分页
     * @param sqlSessionFactory
     */
    public PageUtil<TbDoctor> getDoctorPage(int pageIndex,int pageSize){
    	SqlSession sqlSession = null;
    	List<TbDoctor> list = null;
    	int count = 0;//总记录数
    	try {
			sqlSession = sqlSessionFactory.openSession();
			TbDoctorMapper mapper = sqlSession.getMapper(TbDoctorMapper.class);
			count = mapper.getCount();
			
			Map<String, Integer> map = new HashMap<>();
			map.put("begin", (pageIndex-1)*pageSize);
			map.put("end", pageSize-1);getClass();
			list = mapper.search(map);
		} catch (Exception e) {
			System.out.println(e.getMessage());
		} finally {
			sqlSession.close();
		}
    	//封装分页数据
    	pageUtil.setPageSize(pageSize);
		pageUtil.setPageIndex(pageIndex);
		pageUtil.setCount(count);
		pageUtil.setPageNumber(count%pageSize==0?count/pageSize:count/pageSize+1);
		pageUtil.setList(list);
		return pageUtil;
    }
    
    /**
     * 获得所有医生信息
     * @param 
     */
    public List<TbDoctor> selectAll() {
        List<TbDoctor> list;
        try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
            TbDoctorMapper mapper = sqlSession.getMapper(TbDoctorMapper.class);
            list = mapper.selectAll();
        }
        return list;
    }

   public TbDoctor findById(Integer id) {
        TbDoctor tbDoctor = null;
        try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
            TbDoctorMapper mapper = sqlSession.getMapper(TbDoctorMapper.class);
            tbDoctor = mapper.selectByPrimaryKey(id);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return tbDoctor;
    }

    @Transactional(rollbackFor = Exception.class)
    public int add(TbDoctor tbDoctor) {
        try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
            TbDoctorMapper mapper = sqlSession.getMapper(TbDoctorMapper.class);
            return mapper.insertSelective(tbDoctor);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return 0;
    }

    @Transactional(rollbackFor = Exception.class)
    public int update(TbDoctor tbDoctor) {
        try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
            TbDoctorMapper mapper = sqlSession.getMapper(TbDoctorMapper.class);
            if (tbDoctor.getDoctorId()!=null) {
            	return mapper.updateByPrimaryKeySelective(tbDoctor);
			} else {
				return mapper.insertSelective(tbDoctor);
			}
            
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return 0;
    }

    @Transactional(rollbackFor = Exception.class)
    public int delete(Integer id) {
        try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
            TbDoctorMapper mapper = sqlSession.getMapper(TbDoctorMapper.class);
            return mapper.deleteById(id);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return 0;
    }
}
